{
 "cells": [
  {
   "cell_type": "code",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:38.024243Z",
     "start_time": "2025-01-23T07:42:38.021017Z"
    }
   },
   "source": [
    "import time\n",
    "from sklearn.datasets import load_iris, fetch_20newsgroups, fetch_california_housing\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.metrics import roc_auc_score"
   ],
   "outputs": [],
   "execution_count": 62
  },
  {
   "cell_type": "code",
   "source": [
    "data = pd.read_csv(\"./data/FBlocation/train.csv\")\n",
    "print(data.head(10))\n",
    "print(data.shape)\n",
    "print(data.info())\n",
    "data = data.query(\"x > 1.0 &  x < 1.25 & y > 2.5 & y < 2.75\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.799838Z",
     "start_time": "2025-01-23T07:42:38.104393Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   row_id       x       y  accuracy    time    place_id\n",
      "0       0  0.7941  9.0809        54  470702  8523065625\n",
      "1       1  5.9567  4.7968        13  186555  1757726713\n",
      "2       2  8.3078  7.0407        74  322648  1137537235\n",
      "3       3  7.3665  2.5165        65  704587  6567393236\n",
      "4       4  4.0961  1.1307        31  472130  7440663949\n",
      "5       5  3.8099  1.9586        75  178065  6289802927\n",
      "6       6  6.3336  4.3720        13  666829  9931249544\n",
      "7       7  5.7409  6.7697        85  369002  5662813655\n",
      "8       8  4.3114  6.9410         3  166384  8471780938\n",
      "9       9  6.3414  0.0758        65  400060  1253803156\n",
      "(29118021, 6)\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 29118021 entries, 0 to 29118020\n",
      "Data columns (total 6 columns):\n",
      " #   Column    Dtype  \n",
      "---  ------    -----  \n",
      " 0   row_id    int64  \n",
      " 1   x         float64\n",
      " 2   y         float64\n",
      " 3   accuracy  int64  \n",
      " 4   time      int64  \n",
      " 5   place_id  int64  \n",
      "dtypes: float64(2), int64(4)\n",
      "memory usage: 1.3 GB\n",
      "None\n"
     ]
    }
   ],
   "execution_count": 63
  },
  {
   "cell_type": "code",
   "source": [
    "data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.804840Z",
     "start_time": "2025-01-23T07:42:47.800839Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(17710, 6)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 64
  },
  {
   "cell_type": "code",
   "source": [
    "data.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.835730Z",
     "start_time": "2025-01-23T07:42:47.805841Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             row_id             x             y      accuracy           time  \\\n",
       "count  1.771000e+04  17710.000000  17710.000000  17710.000000   17710.000000   \n",
       "mean   1.450569e+07      1.122538      2.632309     82.482101  397551.263128   \n",
       "std    8.353805e+06      0.077086      0.070144    113.613227  234601.097883   \n",
       "min    6.000000e+02      1.000100      2.500100      1.000000     119.000000   \n",
       "25%    7.327816e+06      1.049200      2.573800     25.000000  174069.750000   \n",
       "50%    1.443071e+07      1.123300      2.642300     62.000000  403387.500000   \n",
       "75%    2.163463e+07      1.190500      2.687800     75.000000  602111.750000   \n",
       "max    2.911215e+07      1.249900      2.749900   1004.000000  786218.000000   \n",
       "\n",
       "           place_id  \n",
       "count  1.771000e+04  \n",
       "mean   5.129895e+09  \n",
       "std    2.357399e+09  \n",
       "min    1.012024e+09  \n",
       "25%    3.312464e+09  \n",
       "50%    5.261906e+09  \n",
       "75%    6.766325e+09  \n",
       "max    9.980711e+09  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.771000e+04</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>1.771000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.450569e+07</td>\n",
       "      <td>1.122538</td>\n",
       "      <td>2.632309</td>\n",
       "      <td>82.482101</td>\n",
       "      <td>397551.263128</td>\n",
       "      <td>5.129895e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.353805e+06</td>\n",
       "      <td>0.077086</td>\n",
       "      <td>0.070144</td>\n",
       "      <td>113.613227</td>\n",
       "      <td>234601.097883</td>\n",
       "      <td>2.357399e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>6.000000e+02</td>\n",
       "      <td>1.000100</td>\n",
       "      <td>2.500100</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>119.000000</td>\n",
       "      <td>1.012024e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.327816e+06</td>\n",
       "      <td>1.049200</td>\n",
       "      <td>2.573800</td>\n",
       "      <td>25.000000</td>\n",
       "      <td>174069.750000</td>\n",
       "      <td>3.312464e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.443071e+07</td>\n",
       "      <td>1.123300</td>\n",
       "      <td>2.642300</td>\n",
       "      <td>62.000000</td>\n",
       "      <td>403387.500000</td>\n",
       "      <td>5.261906e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.163463e+07</td>\n",
       "      <td>1.190500</td>\n",
       "      <td>2.687800</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>602111.750000</td>\n",
       "      <td>6.766325e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.911215e+07</td>\n",
       "      <td>1.249900</td>\n",
       "      <td>2.749900</td>\n",
       "      <td>1004.000000</td>\n",
       "      <td>786218.000000</td>\n",
       "      <td>9.980711e+09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 65
  },
  {
   "cell_type": "code",
   "source": [
    "time_value = pd.to_datetime(data['time'], unit='s')\n",
    "print(time_value.head(10)) "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.842017Z",
     "start_time": "2025-01-23T07:42:47.836733Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "600    1970-01-01 18:09:40\n",
      "957    1970-01-10 02:11:10\n",
      "4345   1970-01-05 15:08:02\n",
      "4735   1970-01-06 23:03:03\n",
      "5580   1970-01-09 11:26:50\n",
      "6090   1970-01-02 16:25:07\n",
      "6234   1970-01-04 15:52:57\n",
      "6350   1970-01-01 10:13:36\n",
      "7468   1970-01-09 15:26:06\n",
      "8478   1970-01-08 23:52:02\n",
      "Name: time, dtype: datetime64[ns]\n"
     ]
    }
   ],
   "execution_count": 66
  },
  {
   "cell_type": "code",
   "source": [
    "time_value = pd.DatetimeIndex(time_value)\n",
    "print(time_value[0:10])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.845653Z",
     "start_time": "2025-01-23T07:42:47.843019Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['1970-01-01 18:09:40', '1970-01-10 02:11:10',\n",
      "               '1970-01-05 15:08:02', '1970-01-06 23:03:03',\n",
      "               '1970-01-09 11:26:50', '1970-01-02 16:25:07',\n",
      "               '1970-01-04 15:52:57', '1970-01-01 10:13:36',\n",
      "               '1970-01-09 15:26:06', '1970-01-08 23:52:02'],\n",
      "              dtype='datetime64[ns]', name='time', freq=None)\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "cell_type": "code",
   "source": [
    "data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.849462Z",
     "start_time": "2025-01-23T07:42:47.845653Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(17710, 6)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 68
  },
  {
   "cell_type": "code",
   "source": [
    "print(type(data))\n",
    "data.insert(data.shape[1], 'day', time_value.day) \n",
    "data.insert(data.shape[1], 'hour', time_value.hour)\n",
    "data.insert(data.shape[1], 'weekday', time_value.weekday) \n",
    "data = data.drop(['time'], axis=1)\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.860436Z",
     "start_time": "2025-01-23T07:42:47.850466Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "      row_id       x       y  accuracy    place_id  day  hour  weekday\n",
       "600      600  1.2214  2.7023        17  6683426742    1    18        3\n",
       "957      957  1.1832  2.6891        58  6683426742   10     2        5\n",
       "4345    4345  1.1935  2.6550        11  6889790653    5    15        0\n",
       "4735    4735  1.1452  2.6074        49  6822359752    6    23        1\n",
       "5580    5580  1.0089  2.7287        19  1527921905    9    11        4"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>place_id</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>600</th>\n",
       "      <td>600</td>\n",
       "      <td>1.2214</td>\n",
       "      <td>2.7023</td>\n",
       "      <td>17</td>\n",
       "      <td>6683426742</td>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>957</th>\n",
       "      <td>957</td>\n",
       "      <td>1.1832</td>\n",
       "      <td>2.6891</td>\n",
       "      <td>58</td>\n",
       "      <td>6683426742</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4345</th>\n",
       "      <td>4345</td>\n",
       "      <td>1.1935</td>\n",
       "      <td>2.6550</td>\n",
       "      <td>11</td>\n",
       "      <td>6889790653</td>\n",
       "      <td>5</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4735</th>\n",
       "      <td>4735</td>\n",
       "      <td>1.1452</td>\n",
       "      <td>2.6074</td>\n",
       "      <td>49</td>\n",
       "      <td>6822359752</td>\n",
       "      <td>6</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5580</th>\n",
       "      <td>5580</td>\n",
       "      <td>1.0089</td>\n",
       "      <td>2.7287</td>\n",
       "      <td>19</td>\n",
       "      <td>1527921905</td>\n",
       "      <td>9</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 69
  },
  {
   "cell_type": "code",
   "source": [
    "per = pd.Period('2025-01-19 18:00', 'h')\n",
    "per.weekday"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.864853Z",
     "start_time": "2025-01-23T07:42:47.860436Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 70
  },
  {
   "cell_type": "code",
   "source": "data.describe()",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.881601Z",
     "start_time": "2025-01-23T07:42:47.864853Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             row_id             x             y      accuracy      place_id  \\\n",
       "count  1.771000e+04  17710.000000  17710.000000  17710.000000  1.771000e+04   \n",
       "mean   1.450569e+07      1.122538      2.632309     82.482101  5.129895e+09   \n",
       "std    8.353805e+06      0.077086      0.070144    113.613227  2.357399e+09   \n",
       "min    6.000000e+02      1.000100      2.500100      1.000000  1.012024e+09   \n",
       "25%    7.327816e+06      1.049200      2.573800     25.000000  3.312464e+09   \n",
       "50%    1.443071e+07      1.123300      2.642300     62.000000  5.261906e+09   \n",
       "75%    2.163463e+07      1.190500      2.687800     75.000000  6.766325e+09   \n",
       "max    2.911215e+07      1.249900      2.749900   1004.000000  9.980711e+09   \n",
       "\n",
       "                day          hour       weekday  \n",
       "count  17710.000000  17710.000000  17710.000000  \n",
       "mean       5.101863     11.485545      3.092377  \n",
       "std        2.709287      6.932195      1.680218  \n",
       "min        1.000000      0.000000      0.000000  \n",
       "25%        3.000000      6.000000      2.000000  \n",
       "50%        5.000000     12.000000      3.000000  \n",
       "75%        7.000000     17.000000      4.000000  \n",
       "max       10.000000     23.000000      6.000000  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>place_id</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.771000e+04</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>1.771000e+04</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.450569e+07</td>\n",
       "      <td>1.122538</td>\n",
       "      <td>2.632309</td>\n",
       "      <td>82.482101</td>\n",
       "      <td>5.129895e+09</td>\n",
       "      <td>5.101863</td>\n",
       "      <td>11.485545</td>\n",
       "      <td>3.092377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.353805e+06</td>\n",
       "      <td>0.077086</td>\n",
       "      <td>0.070144</td>\n",
       "      <td>113.613227</td>\n",
       "      <td>2.357399e+09</td>\n",
       "      <td>2.709287</td>\n",
       "      <td>6.932195</td>\n",
       "      <td>1.680218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>6.000000e+02</td>\n",
       "      <td>1.000100</td>\n",
       "      <td>2.500100</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.012024e+09</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.327816e+06</td>\n",
       "      <td>1.049200</td>\n",
       "      <td>2.573800</td>\n",
       "      <td>25.000000</td>\n",
       "      <td>3.312464e+09</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.443071e+07</td>\n",
       "      <td>1.123300</td>\n",
       "      <td>2.642300</td>\n",
       "      <td>62.000000</td>\n",
       "      <td>5.261906e+09</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.163463e+07</td>\n",
       "      <td>1.190500</td>\n",
       "      <td>2.687800</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>6.766325e+09</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>17.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.911215e+07</td>\n",
       "      <td>1.249900</td>\n",
       "      <td>2.749900</td>\n",
       "      <td>1004.000000</td>\n",
       "      <td>9.980711e+09</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>23.000000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 71
  },
  {
   "cell_type": "code",
   "source": [
    "place_count = data.groupby('place_id').count()\n",
    "place_count"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.890165Z",
     "start_time": "2025-01-23T07:42:47.882597Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            row_id     x     y  accuracy   day  hour  weekday\n",
       "place_id                                                     \n",
       "1012023972       1     1     1         1     1     1        1\n",
       "1057182134       1     1     1         1     1     1        1\n",
       "1059958036       3     3     3         3     3     3        3\n",
       "1085266789       1     1     1         1     1     1        1\n",
       "1097200869    1044  1044  1044      1044  1044  1044     1044\n",
       "...            ...   ...   ...       ...   ...   ...      ...\n",
       "9904182060       1     1     1         1     1     1        1\n",
       "9915093501       1     1     1         1     1     1        1\n",
       "9946198589       1     1     1         1     1     1        1\n",
       "9950190890       1     1     1         1     1     1        1\n",
       "9980711012       5     5     5         5     5     5        5\n",
       "\n",
       "[805 rows x 7 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>place_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1012023972</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1057182134</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1059958036</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1085266789</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1097200869</th>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9904182060</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9915093501</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9946198589</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9950190890</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9980711012</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>805 rows × 7 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 72
  },
  {
   "cell_type": "code",
   "source": "place_count['x'].describe()",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.895808Z",
     "start_time": "2025-01-23T07:42:47.890165Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     805.000000\n",
       "mean       22.000000\n",
       "std        88.955632\n",
       "min         1.000000\n",
       "25%         1.000000\n",
       "50%         2.000000\n",
       "75%         5.000000\n",
       "max      1044.000000\n",
       "Name: x, dtype: float64"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 73
  },
  {
   "cell_type": "code",
   "source": [
    "tf = place_count[place_count.row_id > 3].reset_index()\n",
    "tf"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.904151Z",
     "start_time": "2025-01-23T07:42:47.896810Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "       place_id  row_id     x     y  accuracy   day  hour  weekday\n",
       "0    1097200869    1044  1044  1044      1044  1044  1044     1044\n",
       "1    1228935308     120   120   120       120   120   120      120\n",
       "2    1267801529      58    58    58        58    58    58       58\n",
       "3    1278040507      15    15    15        15    15    15       15\n",
       "4    1285051622      21    21    21        21    21    21       21\n",
       "..          ...     ...   ...   ...       ...   ...   ...      ...\n",
       "234  9741307878       5     5     5         5     5     5        5\n",
       "235  9753855529      21    21    21        21    21    21       21\n",
       "236  9806043737       6     6     6         6     6     6        6\n",
       "237  9809476069      23    23    23        23    23    23       23\n",
       "238  9980711012       5     5     5         5     5     5        5\n",
       "\n",
       "[239 rows x 8 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>place_id</th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1097200869</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1228935308</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1267801529</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1278040507</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1285051622</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>234</th>\n",
       "      <td>9741307878</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>235</th>\n",
       "      <td>9753855529</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>236</th>\n",
       "      <td>9806043737</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>237</th>\n",
       "      <td>9809476069</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>238</th>\n",
       "      <td>9980711012</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>239 rows × 8 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 74
  },
  {
   "cell_type": "code",
   "source": [
    "data = data[data['place_id'].isin(tf.place_id)]\n",
    "data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.909144Z",
     "start_time": "2025-01-23T07:42:47.905152Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(16918, 8)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 75
  },
  {
   "cell_type": "code",
   "source": [
    "y = data['place_id']\n",
    "x = data.drop(['place_id'], axis=1)\n",
    "x = x.drop(['row_id'], axis=1)\n",
    "print(x.shape)\n",
    "print(x.columns)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.913731Z",
     "start_time": "2025-01-23T07:42:47.909144Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(16918, 6)\n",
      "Index(['x', 'y', 'accuracy', 'day', 'hour', 'weekday'], dtype='object')\n"
     ]
    }
   ],
   "execution_count": 76
  },
  {
   "cell_type": "code",
   "source": [
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=1)\n",
    "std = StandardScaler()\n",
    "x_train = std.fit_transform(x_train)\n",
    "print(std.mean_)\n",
    "print(std.var_)\n",
    "x_test = std.transform(x_test)  \n",
    "print('-' * 50)\n",
    "print(std.mean_)\n",
    "print(std.var_)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.923971Z",
     "start_time": "2025-01-23T07:42:47.913731Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.12295735  2.63237278 81.34938525  5.10064628 11.44293821  3.10135561]\n",
      "[5.98489138e-03 4.86857391e-03 1.19597480e+04 7.32837915e+00\n",
      " 4.83742660e+01 2.81838404e+00]\n",
      "--------------------------------------------------\n",
      "[ 1.12295735  2.63237278 81.34938525  5.10064628 11.44293821  3.10135561]\n",
      "[5.98489138e-03 4.86857391e-03 1.19597480e+04 7.32837915e+00\n",
      " 4.83742660e+01 2.81838404e+00]\n"
     ]
    }
   ],
   "execution_count": 77
  },
  {
   "cell_type": "code",
   "source": [
    "x_train.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:47.927763Z",
     "start_time": "2025-01-23T07:42:47.923971Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12688, 6)"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 78
  },
  {
   "cell_type": "code",
   "source": [
    "knn = KNeighborsClassifier(n_neighbors=6)\n",
    "knn.fit(x_train, y_train)\n",
    "y_predict = knn.predict(x_test)\n",
    "print(\"预测的目标签到位置为：\", y_predict[0:10])\n",
    "print(\"预测的准确率:\", knn.score(x_test, y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:48.235788Z",
     "start_time": "2025-01-23T07:42:47.928764Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的目标签到位置为： [5689129232 1097200869 2355236719 9632980559 6424972551 4022692381\n",
      " 8048985799 6683426742 1435128522 3312463746]\n",
      "预测的准确率: 0.484160756501182\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "cell_type": "code",
   "source": [
    "print(max(time_value))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:48.247716Z",
     "start_time": "2025-01-23T07:42:48.236791Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1970-01-10 02:23:38\n"
     ]
    }
   ],
   "execution_count": 80
  },
  {
   "cell_type": "code",
   "source": [
    "param = {\"n_neighbors\": [3, 5, 10, 12, 15],'weights':['uniform', 'distance']}\n",
    "gc = GridSearchCV(knn, param_grid=param, cv=3)\n",
    "gc.fit(x_train, y_train)\n",
    "print(\"在测试集上准确率：\", gc.score(x_test, y_test))\n",
    "print(\"在交叉验证当中最好的结果：\", gc.best_score_) \n",
    "print(\"选择最好的模型是：\", gc.best_estimator_) \n",
    "print(\"每个超参数每次交叉验证的结果：\")\n",
    "gc.cv_results_"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:52.150811Z",
     "start_time": "2025-01-23T07:42:48.248226Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Program Files\\Python312\\Lib\\site-packages\\sklearn\\model_selection\\_split.py:805: UserWarning: The least populated class in y has only 1 members, which is less than n_splits=3.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在测试集上准确率： 0.49763593380614657\n",
      "在交叉验证当中最好的结果： 0.4816362349278435\n",
      "选择最好的模型是： KNeighborsClassifier(n_neighbors=12, weights='distance')\n",
      "每个超参数每次交叉验证的结果：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'mean_fit_time': array([0.01133617, 0.01066963, 0.01033537, 0.01333618, 0.01733724,\n",
       "        0.01033545, 0.01100238, 0.01000245, 0.01200263, 0.01066939]),\n",
       " 'std_fit_time': array([9.42965571e-04, 4.71988518e-04, 4.71426560e-04, 4.02857397e-03,\n",
       "        7.71912995e-03, 4.71707569e-04, 8.16632101e-04, 4.49566384e-07,\n",
       "        2.16049500e-03, 4.71819960e-04]),\n",
       " 'mean_score_time': array([0.13036211, 0.04768332, 0.12536184, 0.06450566, 0.17318813,\n",
       "        0.07868489, 0.14110796, 0.07901764, 0.20737998, 0.09135405]),\n",
       " 'std_score_time': array([0.00899567, 0.00124884, 0.00385951, 0.00804292, 0.02348743,\n",
       "        0.00601971, 0.00444704, 0.00141428, 0.04027782, 0.00188587]),\n",
       " 'param_n_neighbors': masked_array(data=[3, 3, 5, 5, 10, 10, 12, 12, 15, 15],\n",
       "              mask=[False, False, False, False, False, False, False, False,\n",
       "                    False, False],\n",
       "        fill_value=999999),\n",
       " 'param_weights': masked_array(data=['uniform', 'distance', 'uniform', 'distance',\n",
       "                    'uniform', 'distance', 'uniform', 'distance',\n",
       "                    'uniform', 'distance'],\n",
       "              mask=[False, False, False, False, False, False, False, False,\n",
       "                    False, False],\n",
       "        fill_value=np.str_('?'),\n",
       "             dtype=object),\n",
       " 'params': [{'n_neighbors': 3, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 3, 'weights': 'distance'},\n",
       "  {'n_neighbors': 5, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 5, 'weights': 'distance'},\n",
       "  {'n_neighbors': 10, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 10, 'weights': 'distance'},\n",
       "  {'n_neighbors': 12, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 12, 'weights': 'distance'},\n",
       "  {'n_neighbors': 15, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 15, 'weights': 'distance'}],\n",
       " 'split0_test_score': array([0.44468085, 0.4534279 , 0.4607565 , 0.47399527, 0.46170213,\n",
       "        0.48014184, 0.45650118, 0.48108747, 0.45508274, 0.47895981]),\n",
       " 'split1_test_score': array([0.43390873, 0.4542445 , 0.45660913, 0.47528967, 0.45542681,\n",
       "        0.48238354, 0.45329865, 0.48049184, 0.44809648, 0.47623552]),\n",
       " 'split2_test_score': array([0.43982029, 0.4561362 , 0.45684559, 0.47221565, 0.4618113 ,\n",
       "        0.48191062, 0.45897375, 0.48332939, 0.46062899, 0.48049184]),\n",
       " 'mean_test_score': array([0.43946996, 0.45460287, 0.45807041, 0.47383353, 0.45964675,\n",
       "        0.48147867, 0.45625786, 0.48163623, 0.45460274, 0.47856239]),\n",
       " 'std_test_score': array([0.00440467, 0.00113433, 0.00190181, 0.00126016, 0.00298428,\n",
       "        0.00096479, 0.00232323, 0.00122169, 0.00512762, 0.00176021]),\n",
       " 'rank_test_score': array([10,  8,  6,  4,  5,  2,  7,  1,  9,  3], dtype=int32)}"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 81
  },
  {
   "cell_type": "code",
   "source": [
    "news = fetch_20newsgroups(subset='all', data_home='data')\n",
    "print(len(news.data))  \n",
    "print('-'*50)\n",
    "print(news.data[0]) \n",
    "print('-'*50)\n",
    "print(news.target) \n",
    "print(np.unique(news.target)) \n",
    "print(news.target_names) "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:52.326457Z",
     "start_time": "2025-01-23T07:42:52.150811Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18846\n",
      "--------------------------------------------------\n",
      "From: Mamatha Devineni Ratnam <mr47+@andrew.cmu.edu>\n",
      "Subject: Pens fans reactions\n",
      "Organization: Post Office, Carnegie Mellon, Pittsburgh, PA\n",
      "Lines: 12\n",
      "NNTP-Posting-Host: po4.andrew.cmu.edu\n",
      "\n",
      "\n",
      "\n",
      "I am sure some bashers of Pens fans are pretty confused about the lack\n",
      "of any kind of posts about the recent Pens massacre of the Devils. Actually,\n",
      "I am  bit puzzled too and a bit relieved. However, I am going to put an end\n",
      "to non-PIttsburghers' relief with a bit of praise for the Pens. Man, they\n",
      "are killing those Devils worse than I thought. Jagr just showed you why\n",
      "he is much better than his regular season stats. He is also a lot\n",
      "fo fun to watch in the playoffs. Bowman should let JAgr have a lot of\n",
      "fun in the next couple of games since the Pens are going to beat the pulp out of Jersey anyway. I was very disappointed not to see the Islanders lose the final\n",
      "regular season game.          PENS RULE!!!\n",
      "\n",
      "\n",
      "--------------------------------------------------\n",
      "[10  3 17 ...  3  1  7]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]\n",
      "['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc']\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "cell_type": "code",
   "source": [
    "x_train, x_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25, random_state=1)\n",
    "tf = TfidfVectorizer()\n",
    "x_train = tf.fit_transform(x_train)\n",
    "print(len(tf.get_feature_names_out()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:55.014256Z",
     "start_time": "2025-01-23T07:42:52.326457Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153196\n"
     ]
    }
   ],
   "execution_count": 83
  },
  {
   "cell_type": "code",
   "source": [
    "print(tf.get_feature_names_out()[100000])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:55.136701Z",
     "start_time": "2025-01-23T07:42:55.015250Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "murky\n"
     ]
    }
   ],
   "execution_count": 84
  },
  {
   "cell_type": "code",
   "source": [
    "print(tf.get_feature_names_out()[0:10])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:55.247800Z",
     "start_time": "2025-01-23T07:42:55.137703Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['00' '000' '0000' '00000' '0000000004' '0000000005' '0000000667'\n",
      " '0000001200' '000003' '000005102000']\n"
     ]
    }
   ],
   "execution_count": 85
  },
  {
   "cell_type": "code",
   "source": [
    "import time\n",
    "mlt = MultinomialNB(alpha=1.0)\n",
    "start=time.time()\n",
    "mlt.fit(x_train, y_train)\n",
    "end=time.time()\n",
    "end-start "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:55.364463Z",
     "start_time": "2025-01-23T07:42:55.248801Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.10802459716796875"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 86
  },
  {
   "cell_type": "code",
   "source": [
    "x_transform_test = tf.transform(x_test) \n",
    "print(len(tf.get_feature_names_out())) "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.345467Z",
     "start_time": "2025-01-23T07:42:55.364463Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153196\n"
     ]
    }
   ],
   "execution_count": 87
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.349470Z",
     "start_time": "2025-01-23T07:42:56.345467Z"
    }
   },
   "cell_type": "code",
   "source": "x_transform_test.shape",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4712, 153196)"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 88
  },
  {
   "cell_type": "code",
   "source": [
    "start=time.time()\n",
    "y_predict = mlt.predict(x_transform_test)\n",
    "print(\"预测的前面10篇文章类别为：\", y_predict[0:10])\n",
    "print(\"准确率为：\", mlt.score(x_transform_test, y_test))\n",
    "end=time.time()\n",
    "end-start"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.417288Z",
     "start_time": "2025-01-23T07:42:56.349470Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的前面10篇文章类别为： [16 19 18  1  9 15  1  2 16 13]\n",
      "准确率为： 0.8518675721561969\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.046010732650756836"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 89
  },
  {
   "cell_type": "code",
   "source": "len(y_predict)",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.422488Z",
     "start_time": "2025-01-23T07:42:56.419284Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4712"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 90
  },
  {
   "cell_type": "code",
   "source": [
    "print(classification_report(y_test, y_predict,\n",
    "      target_names=news.target_names))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.430787Z",
     "start_time": "2025-01-23T07:42:56.422488Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.91      0.77      0.83       199\n",
      "           comp.graphics       0.83      0.79      0.81       242\n",
      " comp.os.ms-windows.misc       0.89      0.83      0.86       263\n",
      "comp.sys.ibm.pc.hardware       0.80      0.83      0.81       262\n",
      "   comp.sys.mac.hardware       0.90      0.88      0.89       234\n",
      "          comp.windows.x       0.92      0.85      0.88       230\n",
      "            misc.forsale       0.96      0.67      0.79       257\n",
      "               rec.autos       0.90      0.87      0.88       265\n",
      "         rec.motorcycles       0.90      0.95      0.92       251\n",
      "      rec.sport.baseball       0.89      0.96      0.93       226\n",
      "        rec.sport.hockey       0.95      0.98      0.96       262\n",
      "               sci.crypt       0.76      0.97      0.85       257\n",
      "         sci.electronics       0.84      0.80      0.82       229\n",
      "                 sci.med       0.97      0.86      0.91       249\n",
      "               sci.space       0.92      0.96      0.94       256\n",
      "  soc.religion.christian       0.55      0.98      0.70       243\n",
      "      talk.politics.guns       0.76      0.96      0.85       234\n",
      "   talk.politics.mideast       0.93      0.99      0.96       224\n",
      "      talk.politics.misc       0.98      0.56      0.72       197\n",
      "      talk.religion.misc       0.97      0.26      0.41       132\n",
      "\n",
      "                accuracy                           0.85      4712\n",
      "               macro avg       0.88      0.84      0.84      4712\n",
      "            weighted avg       0.87      0.85      0.85      4712\n",
      "\n"
     ]
    }
   ],
   "execution_count": 91
  },
  {
   "cell_type": "code",
   "source": "y_test.shape ",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.434582Z",
     "start_time": "2025-01-23T07:42:56.430787Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4712,)"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 92
  },
  {
   "cell_type": "code",
   "source": [
    "y_test1 = np.where(y_test == 0, 1, 0)\n",
    "print(y_test1.sum()) "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.437862Z",
     "start_time": "2025-01-23T07:42:56.434582Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "199\n"
     ]
    }
   ],
   "execution_count": 93
  },
  {
   "cell_type": "code",
   "source": [
    "y_predict1 = np.where(y_predict == 0, 1, 0)\n",
    "print(y_predict1.sum())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.441372Z",
     "start_time": "2025-01-23T07:42:56.437862Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "168\n"
     ]
    }
   ],
   "execution_count": 94
  },
  {
   "cell_type": "code",
   "source": [
    "(y_test1*y_predict1).sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.445123Z",
     "start_time": "2025-01-23T07:42:56.441372Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(153)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 95
  },
  {
   "cell_type": "code",
   "source": [
    "153/168"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.448753Z",
     "start_time": "2025-01-23T07:42:56.445123Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9107142857142857"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 96
  },
  {
   "cell_type": "code",
   "source": [
    "153/199"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.452272Z",
     "start_time": "2025-01-23T07:42:56.448753Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7688442211055276"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 97
  },
  {
   "cell_type": "code",
   "source": [
    "max(y_test),min(y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.456130Z",
     "start_time": "2025-01-23T07:42:56.452272Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.int32(19), np.int32(0))"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 98
  },
  {
   "cell_type": "code",
   "source": [
    "y_test1 = np.where(y_test == 5, 1, 0)\n",
    "print(y_test1.sum()) \n",
    "y_predict1 = np.where(y_predict == 5, 1, 0)\n",
    "print(y_predict1.sum())\n",
    "print(\"AUC指标：\", roc_auc_score(y_test1, y_predict1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.461283Z",
     "start_time": "2025-01-23T07:42:56.456130Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "230\n",
      "214\n",
      "AUC指标： 0.924078924393225\n"
     ]
    }
   ],
   "execution_count": 99
  },
  {
   "cell_type": "code",
   "source": [
    "y_test1,y_predict1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.465011Z",
     "start_time": "2025-01-23T07:42:56.461283Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 0, 0, ..., 0, 0, 0], shape=(4712,)),\n",
       " array([0, 0, 0, ..., 0, 0, 0], shape=(4712,)))"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 100
  },
  {
   "cell_type": "code",
   "source": [
    "FP=np.where((np.array(y_test1)-np.array(y_predict1))==-1,1,0).sum() \n",
    "TP=y_predict1.sum()-FP \n",
    "print(TP)\n",
    "FN=np.where((np.array(y_test1)-np.array(y_predict1))==1,1,0).sum()\n",
    "print(FN)\n",
    "TN=np.where(y_test1==0,1,0).sum()-FP  \n",
    "print(TN)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.469139Z",
     "start_time": "2025-01-23T07:42:56.465011Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "196\n",
      "34\n",
      "4464\n"
     ]
    }
   ],
   "execution_count": 101
  },
  {
   "cell_type": "code",
   "source": "TP/(TP+FP) ",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.472815Z",
     "start_time": "2025-01-23T07:42:56.469139Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.9158878504672897)"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 102
  },
  {
   "cell_type": "code",
   "source": "TP/(TP+FN) ",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.476375Z",
     "start_time": "2025-01-23T07:42:56.472815Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.8521739130434782)"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 103
  },
  {
   "cell_type": "code",
   "source": "2*TP/(2*TP+FP+FN)",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.479757Z",
     "start_time": "2025-01-23T07:42:56.476375Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.8828828828828829)"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 104
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "del news\n",
    "del x_train\n",
    "del x_test\n",
    "del y_test\n",
    "del y_predict\n",
    "del tf"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.503818Z",
     "start_time": "2025-01-23T07:42:56.479757Z"
    }
   },
   "outputs": [],
   "execution_count": 105
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-23T07:42:56.506648Z",
     "start_time": "2025-01-23T07:42:56.504819Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "outputs": [],
   "execution_count": 105
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
